<template>
  <div>
    <el-button type="primary" icon="el-icon-plus" @click="addTradeMark"
      >添加</el-button
    >
    <!-- 表格区域:显示品牌信息
        border设置边框,header-call-style设置表头的样式外观

    -->
    <el-table
      style="width: 100%; margin: 10px 0"
      border
      :header-cell-style="{ color: 'red' }"
      :data="records"
    >
      <el-table-column type="index" label="序号" width="80px" align="center">
      </el-table-column>
      <el-table-column prop="tmName" label="品牌名称" width="width">
      </el-table-column>
      <el-table-column label="品牌LOGO" width="width">
        <!-- 作用域卡槽:table里面早已经留坑了,只需填坑 -->
        <template slot-scope="{ row, $index }">
          <img :src="row.logoUrl" alt="" style="width: 80px; height: 80px" />
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{ row, $index }">
          <el-button
            type="warning"
            icon="el-icon-edit"
            style="margin-right:10px"
            @click="UpdateTrademark(row)"
            >修改</el-button
          >


          <!-- <el-button
            type="danger"
            icon="el-icon-delete"
            @click="DeleteTrademark(row.id)"
            >删除</el-button
          > -->
           <el-popconfirm title="这是一段内容确定删除吗？" icon="el-icon-delete" icon-color="#409EFF" @onConfirm="DeleteTrademark(row.id)">
            <el-button
              type="danger"
              icon="el-icon-delete"
              slot="reference"
              >删除</el-button
            >
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>
    <!-- 
        分页器

     -->
    <el-pagination
      style="text-align: center"
      @current-change="getTrademarkList"
      @size-change="handleSizeChange"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      background
      next-text="下一页"
      prev-text="上一页"
      layout=" prev, pager, next, jumper,->,total, sizes"
      :total="total"
    >
    </el-pagination>

    <!-- 添加品牌|或者修改品牌按钮的时候,弹出的对话框 -->
    <!-- <el-button type="text" @click="dialogFormVisible = true"
      >打开嵌套表单的 Dialog</el-button
    > -->

    <el-dialog
      :title="this.tmForm.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogFormVisible"
      :before-close="close"
      :show-close="false"
    >
      <el-form style="width: 80%" :model="tmForm" :rules="rules" ref="cur">
        <el-form-item label="品牌名称" label-width="80px" prop="tmName">
          <el-input auto-complete="off" v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <!--  -->
        <el-form-item label="品牌LOGO" prop="logoUrl">
          <!-- 上传图片 -->
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <!--  -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateTrademark"
          >确 定</el-button
        >
        <!-- dialogFormVisible = false -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
//
export default {
  name: "Trademark",
  data() {
    // 自定义校验规则
    const validateTmName=(rule,value,callBack)=>{
      //rule:能获取校验字段---没啥用
      //value:value是用户输入进来的内容
      //callBack:放行函数
      // 符合条件放行
      if (/^[\u400-\u9fa5]{2,10}$/.test(value)) {
        callBack();
      }else{
        callBack(new Error("豪哥提示:品牌的名字是汉字2-10位"))
      }
    };
    return {
      //   page:当前第几页
      page: 1,
      // limit:一页展示几条数据
      limit: 3,
      // total:一共多少调数据
      total: 0,
      // 品牌的数据--数组
      records: [],
      // 控制对话框的显示与隐藏--默认隐藏
      dialogFormVisible: false,
      // 上传图片用的属性
      // imageUrl: "",
      //
      tmForm: {
        logoUrl: "",
        tmName: "",
      },
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "change" },
          { validator: validateTmName, trigger: "change" },
        ],
        logoUrl: [{ required: true, message: "请选择品牌LOGO" }],
      },
    };
  },
  // 组件挂载完毕执行一次
  mounted() {
    this.getTrademarkList();
  },
  methods: {
    // 获取品牌的数据(用户选择第几页的回调函数)
    async getTrademarkList(val = 1) {
      this.page = val;
      // 发请求--携带参数page(当前第几页) limit(一页多少条数据)
      // 结构参数
      let { page, limit } = this;
      let result = await this.$ajax.trademark.reqTrademarkList(page, limit);
      // 获取数据成功--存储数据
      if (result.code == 200) {
        // 分页器
        this.total = result.data.total;
        // 品牌数据
        this.records = result.data.records;
      }
    },
    // 当某一页展示数据的个数发生变化的时候就会触发
    handleSizeChange(value) {
      this.limit = value;
      this.getTrademarkList();
    },
    // 添加产品的回调函数
    addTradeMark() {
      // 点击添加品牌按钮显示对话框
      this.dialogFormVisible = true;
      this.tmForm = {
        logoUrl: "",
        tmName: "",
      };
    },
    // 上传图片相关的函数
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
      this.tmForm.logoUrl = res.data;
    },
    // 图片上传之前的回调
    beforeAvatarUpload(file) {
      // 判断上传图片类型
      const isJPG = file.type === "image/jpeg";
      // 判断上传图片的大小
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    // 对话框确定按钮的回调函数
    addOrUpdateTrademark() {
      this.$refs.cur.validate(async (success) => {
        if (success) {
          // 关闭对话框
          this.dialogFormVisible = false;
          try {
            await this.$ajax.trademark.reqAddOrUpdate(this.tmForm);
            this.$message({
              message: this.tmForm.id ? "修改成功" : "添加成功",
              type: "success",
            });
            this.getTrademarkList(this.tmForm.id ? this.page : 1);
            // this.getTrademarkList(this.page);
            // 表单重置
            this.$refs.cur.resetFields();
          } catch (error) {
            this.$message({
              message: this.tmForm.id ? "修改失败" : "添加失败",
              type: "error",
            });
          }
        }else{
          // 表单未通过
          return false
        }
      });
    },

    // 修改
    UpdateTrademark(row) {
      // console.log(row);
      this.dialogFormVisible = true;
      this.tmForm = { ...row };
    },
    // 取消(对话框的取消按钮)
    cancel() {
      // 关闭对话框
      this.dialogFormVisible = false;
      //
    },
    // 关闭对话框的监听事件
    close(done) {},

    async DeleteTrademark(id) {
      try {
        await this.$ajax.trademark.reqDeleteDate(id);
        this.$message({
          message: "删除成功",
          type: "success",
        });
        // this.records:当前table 展示品牌的数据
        this.getTrademarkList(this.records.length > 1 ? this.page : this.page - 1);
      } catch (error) {
        this.$message({
          message: "删除失败",
          type: "error",
        });
      }
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>